package com.edu.handler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @Author:zyn
 * @Date:2025/11/10--17:25:04
 */
public class LogHandler implements InvocationHandler {
    private Object obj;

    public LogHandler(Object obj){
        this.obj=obj;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if(method.getName().startsWith("add") || method.getName().startsWith("delete") || method.getName().startsWith("update")){
            System.out.println(method.getName()+"开始执行于："+new Date());
            Object result=method.invoke(obj,args);
            return result;
        }
        if(method.getName().startsWith("select")){
            long start=System.currentTimeMillis();
            Object result=method.invoke(obj,args);
            Thread.sleep(3000);
            long end=System.currentTimeMillis();
            System.out.println(method.getName()+"方法执行耗时："+(end-start));
            return result;
        }
        return method.invoke(obj,args);
    }
}
